package com.onenine.app.view.popwin;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;

import com.onenine.app.R;

public class DynamicPopWindow extends BottomPopWindow implements View.OnClickListener {

    //动态权限
    public static final int DYNAMIC_DELETE = -1;
    public static final int DYNAMIC_SELF_OPEN = DYNAMIC_DELETE + 1;
    public static final int DYNAMIC_FRIEND_OPEN = DYNAMIC_SELF_OPEN + 1;
    public static final int DYNAMIC_ALL_OPEN = DYNAMIC_FRIEND_OPEN + 1;
    public static final String DYNAMIC_DELETE_TEXT = "删除";
    public static final String DYNAMIC_SELF_OPEN_TEXT = "秘密";
    public static final String DYNAMIC_FRIEND_OPEN_TEXT = "好友可见";
    public static final String DYNAMIC_ALL_OPEN_TEXT = "公开";
    private OnItemClickListener mOnItemClick;

    public interface OnItemClickListener {
        void onItemClick(int permission, String permissionDesc);
    }

    /**
     * 转换动态权限描述
     *
     * @return
     */
    public static String convertDynamicPermission(int permission) {
        return permission == DYNAMIC_ALL_OPEN ? DYNAMIC_ALL_OPEN_TEXT
                : permission == DYNAMIC_FRIEND_OPEN ? DYNAMIC_FRIEND_OPEN_TEXT : DYNAMIC_SELF_OPEN_TEXT;
    }

    /**
     * 是否公开
     *
     * @return
     */
    public static boolean isPermissionOpen(int permission) {
        return permission == DYNAMIC_ALL_OPEN;
    }

    /**
     * 获取权限类型组
     *
     * @return
     */
    public static int[] getPermissionTypes() {
        return new int[]{DYNAMIC_ALL_OPEN, DYNAMIC_FRIEND_OPEN, DYNAMIC_SELF_OPEN};
    }

    /**
     * 获取权限描述组
     *
     * @return
     */
    public static String[] getPermissionTexts() {
        return new String[]{DYNAMIC_ALL_OPEN_TEXT, DYNAMIC_FRIEND_OPEN_TEXT, DYNAMIC_SELF_OPEN_TEXT};
    }

    public DynamicPopWindow(Activity activity, OnItemClickListener onItemClick) {
        super(activity);
        mOnItemClick = onItemClick;
    }

    @Override
    protected View genContentView() {
        View bottomView = LayoutInflater.from(mActivity)
                .inflate(R.layout.layout_dynamic_popwin, null, false);
        bottomView.findViewById(R.id.tv_open).setOnClickListener(this);
        bottomView.findViewById(R.id.tv_friend).setOnClickListener(this);
        bottomView.findViewById(R.id.tv_delete).setOnClickListener(this);
        bottomView.findViewById(R.id.tv_secret).setOnClickListener(this);
        return bottomView;
    }

    @Override
    public void onClick(View v) {
        dismiss();
        if (mOnItemClick == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.tv_open:
                mOnItemClick.onItemClick(DYNAMIC_ALL_OPEN, DYNAMIC_ALL_OPEN_TEXT);
                break;
            case R.id.tv_friend:
                mOnItemClick.onItemClick(DYNAMIC_FRIEND_OPEN, DYNAMIC_FRIEND_OPEN_TEXT);
                break;
            case R.id.tv_secret:
                mOnItemClick.onItemClick(DYNAMIC_SELF_OPEN, DYNAMIC_SELF_OPEN_TEXT);
                break;
            case R.id.tv_delete:
                mOnItemClick.onItemClick(DYNAMIC_DELETE, DYNAMIC_DELETE_TEXT);
                break;
        }
    }

}
